package com.senior.english.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.Irr;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.senior.english.config.Constant;
import com.senior.english.model.IrregularVerbModel;

public class ManageFile {

	public void getLastMediaId() {

	}

	public void renameFile(String path, String fileName, String mediaId,
			String type) {
		String fullPath = path + fileName;
		File oldfile = new File(fullPath);
		if (!oldfile.exists()) {
			System.exit(0);
		}
		String newFullPath = path + mediaId + type;
		File newfile = new File(newFullPath);
		oldfile.renameTo(newfile);

	}

	public static List<String> getAllFileNames(String book) {
		List<String> lstName = new ArrayList<String>();
		File file = new File(Constant.grammar + book + "/");
		File[] files = file.listFiles();
		Arrays.sort(files);
		for (File f : files) {
			lstName.add(f.getAbsolutePath().substring(
					f.getAbsolutePath().indexOf("resources")));
		}
		return lstName;
	}
	public static List<String> getAllFileNamesVoc(String book) {
		List<String> lstName = new ArrayList<String>();
		File file = new File(book);
		File[] files = file.listFiles();
		Arrays.sort(files);
		for (File f : files) {
			lstName.add(f.getAbsolutePath().substring(
					f.getAbsolutePath().indexOf("resources")));
		}
		return lstName;
	}

	public static List<IrregularVerbModel> readXSLfile(String xlsFile) {
		List<IrregularVerbModel> lstIVM = new ArrayList<IrregularVerbModel>();
		IrregularVerbModel ivm = null;
		try {

			FileInputStream file = new FileInputStream(new File(xlsFile));

			// Get the workbook instance for XLS file
			HSSFWorkbook workbook = new HSSFWorkbook(file);

			// Get first sheet from the workbook
			HSSFSheet sheet = workbook.getSheetAt(0);

			// Iterate through each rows from first sheet
			Iterator<Row> rowIterator = sheet.iterator();

			while (rowIterator.hasNext()) {
				Row row = rowIterator.next();
				ivm = new IrregularVerbModel();
				int i = 1;
				// For each row, iterate through each columns
				Iterator<Cell> cellIterator = row.cellIterator();
				while (cellIterator.hasNext()) {
					Cell cell = cellIterator.next();
					// System.out.print(cell.getStringCellValue() + "\t\t");
					if (i == 1)
						ivm.setBaseForm(cell.getStringCellValue());
					if (i == 2)
						ivm.setPastParticiple(cell.getStringCellValue());
					if (i == 3)
						ivm.setPastSimple(cell.getStringCellValue());
					if (i == 4)
						ivm.setSingular3rd(cell.getStringCellValue());
					if (i == 5)
						ivm.setGerrund(cell.getStringCellValue());
					i++;

				}
				lstIVM.add(ivm);
				System.out.println("");
			}
			file.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lstIVM;
	}

	public static void main(String[] args) {
		List<IrregularVerbModel> irr = ManageFile.readXSLfile(Constant.irrVerb);
		for (int i = 0; i < irr.size(); i++) {
			System.out.println(irr.get(i).getBaseForm()
					+ irr.get(i).getPastParticiple()
					+ irr.get(i).getPastSimple() + irr.get(i).getSingular3rd()
					+ irr.get(i).getGerrund());
		}
	}
}